cmake_minimum_required(VERSION 3.16)
project(Leetcode)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)

# -- debug 调试信息 --
include_directories(utils)

# 禁用 'dbg.h' 标头包含在代码库中的警告
add_definitions(-DDBG_MACRO_NO_WARNING) 

# 设置调试相关的编译选项
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fsanitize-recover=all -fno-omit-frame-pointer -g")

# -- gtest 测试用例 --
# 指定 gtest 路径和 zip 文件路径
set(GTEST_DIR ${CMAKE_SOURCE_DIR}/third_party/googletest-1.15.2)
set(GTEST_ZIP ${CMAKE_SOURCE_DIR}/third_party/googletest-1.15.2.zip)

# 检查 gtest 文件夹是否存在
if (NOT EXISTS ${GTEST_DIR})
    message(STATUS "GoogleTest not found. Attempting to unpack and build it.")

    # 解压 Googletest
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E tar xzf ${GTEST_ZIP}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/third_party
    )
    if (NOT EXISTS ${GTEST_DIR})
        message(FATAL_ERROR "Failed to unpack Googletest.")
    endif()

    # 构建并安装 Googletest
    execute_process(
        COMMAND cmake -Bbuild -H.
        WORKING_DIRECTORY ${GTEST_DIR}
    )
    execute_process(
        COMMAND make
        WORKING_DIRECTORY ${GTEST_DIR}/build
    )
    execute_process(
        COMMAND sudo make install
        WORKING_DIRECTORY ${GTEST_DIR}/build
    )
endif()

# 添加 Googletest 子目录
if (NOT TARGET gtest)
    add_subdirectory(${GTEST_DIR})
endif()
include_directories(${GTEST_DIR}/googletest/include ${GTEST_DIR}/googlemock/include)

# -- add_executable --
# 查找所有的代码文件
file(GLOB SOURCE_FILES "src/*.cpp")
foreach(SOURCE_FILE ${SOURCE_FILES})
    get_filename_component(FILE_NAME ${SOURCE_FILE} NAME_WE)
    add_executable(${FILE_NAME} ${SOURCE_FILE})
    target_link_libraries(${FILE_NAME} gtest_main)
endforeach()